package com.kikt.commontools;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by kikt on 2016/10/14.
 */

public class SPInstance {
    private static final String name = "default.sp";
    protected static SharedPreferences sharedPreferences;

    private SPInstance() {
    }

    public static SPInstance getInstance(Context context) {
        if (instance == null) {
            synchronized (SPInstance.class) {
                if (instance == null) {
                    instance = new SPInstance();
                    init(context);
                    return instance;
                }
            }
        }
        return instance;
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor editor = getEdit();
        editor.putString(key, value);
        editor.commit();
    }

    public String getString(String key, String defaultValue) {
        return sharedPreferences.getString(key, defaultValue);
    }

    private SharedPreferences.Editor getEdit() {
        return sharedPreferences.edit();
    }

    private static SPInstance instance;

    private static void init(Context context) {
        sharedPreferences = context.getApplicationContext().getSharedPreferences(name, Context.MODE_PRIVATE);
    }
}
